package com.he.hsdyc.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/** 
 * 数学运算辅助类。 
 *  基础信息
 * @author CZH
 * 
 */  
public class BigDecimalUtil {

	private static int type=BigDecimal.ROUND_DOWN;
	
	
	/**
	 * 格式化小数-保留两位,直接截位
	 * @param d
	 * @return
	 */
	public static double formatDouble2Num(Double d) {
        if (d == null) {
            d = 0.0;
        }
        try {
            double temp = (new BigDecimal(d+"").setScale(2, type)).doubleValue();
            return temp;
        }
        catch (Exception e) {
            // TODO: handle exception
            return 0.0;
        }
    }
	
	  /** 
     * 返回两个double的和值 first+second。 
     *  
     * @param first 
     *            第一个double 
     * @param second 
     *            第二个double 
     * @param isformat  是否需要格式化，默认需要
     * @return double 
     * 
     * @author CZH

     */  
    public static double sumDouble(double first, double second,boolean isformat) {  
        BigDecimal b1 = new BigDecimal(first+"");  
        BigDecimal b2 = new BigDecimal(second+"");  
        if(isformat){
        	return formatDouble2Num( b1.add(b2).doubleValue());
	  	}else{
	  		 return  b1.add(b2).doubleValue();  
	  	}
    }  
    public static double sumDouble(double first, double second) {  
    	return sumDouble(first, second, true);
    }  
    
    /** 
     * 返回两个double的差值 first-second。 
     *  
     * @param first 
     *            第一个double 
     * @param second 
     *            第二个double 
     *  @param isformat  是否需要格式化，默认需要
     * @return double 
     * @author CZH

     */  
    public static double subtractDouble(double first, double second,boolean isformat) {  
    	BigDecimal b1 = new BigDecimal(first+"");  
        BigDecimal b2 = new BigDecimal(second+"");  
        if(isformat){
        	return formatDouble2Num( b1.subtract(b2).doubleValue());
	  	}else{
	  		 return  b1.subtract(b2).doubleValue();  
	  	}
    }  
    
    public static double subtractDouble(double first, double second) {  
    	return subtractDouble(first, second, true);
    	
    }  
    
    /** 
     * 返回两个double的乘积 first*second。 
     *  
     * @param first 
     *            第一个double 
     * @param second 
     *            第二个double 
     *  @param isformat  是否需要格式化，默认需要
     * @return double 
     * @author CZH

     */  
    public static double multiplyDouble(double first, double second,boolean isformat) {  
        BigDecimal b1 = new BigDecimal(first+"");  
        BigDecimal b2 = new BigDecimal(second+"");  
        if(isformat){
        	return formatDouble2Num(b1.multiply(b2).doubleValue());
        }else{
        	return b1.multiply(b2).doubleValue();
        }
    }  
    public static double multiplyDouble(double first, double second) {  
    	return multiplyDouble(first, second, true);
    }  
    /** 
     * 返回两个double的商 first除以second。 
     *  
     * @param first 
     *            第一个double 
     * @param second 
     *            第二个double 
     * @return double 
     * @author CZH

     */  
    public static double divideDouble(double first, double second,boolean isformat) {  
        BigDecimal b1 = new BigDecimal(first+"");  
        BigDecimal b2 = new BigDecimal(second+"");  
        if(isformat){
        	return formatDouble2Num((b1.divide(b2, 2, BigDecimal.ROUND_DOWN)).doubleValue());  
        }else{
        	return b1.divide(b2).doubleValue();
        }
    }  
    public static double divideDouble(double first, double second) {  
    	return divideDouble(first, second, true);
    }

    //获取两个date日期的差值，转为数组，年月天
    public static List<String> getDatePoor(Date endDate, Date nowDate) {

        List<String> list = new ArrayList<>();

        long nyear = 365 * 1000 * 24 * 60 * 60L;
        long nmonth = 1000 * 24 * 60 * 60 * 30L;
        long nd = 1000 * 24 * 60 * 60;

        // 获得两个时间的毫秒时间差异
        long diff = nowDate.getTime() - endDate.getTime();

        System.out.println("==================diff:"+diff);
        System.out.println("==================nyear:"+nyear);

        // 计算差多少天
        String year = ""+ diff / nyear;
        String  month = ""+ diff % nyear / nmonth;

        String  day = ""+diff % nyear % nmonth / nd;

        list.add(year);
        list.add("年");
        list.add(month);
        list.add("月");
        list.add(day);
        list.add("天");

       /* String rs = "";
        rs += year + "年";
        rs += month + "月";
        rs += day + "天";*/

        return list;
    }

    //double分解成数组，type区分最后以为数字是否保留两位小数
    public static List<String> typeConversion(Double d) {
        List<String> list = new ArrayList<>();
        String billion = "" + new Double(d/100000000).intValue();
        String tenThousand = "" + new Double((d%100000000)/10000).intValue();

        DecimalFormat    df   = new DecimalFormat("######0.00");
        String yuan = "" + df.format((d%100000000)%10000);

        list.add(billion);
        list.add("亿");
        list.add(tenThousand);
        list.add("万");
        list.add(yuan);
        list.add("元");

        return list;

    }

    public static List<String> typeConversion(Double d,Integer type) {
        List<String> list = new ArrayList<>();
        String billion = "" + new Double(d/100000000).intValue();
        String tenThousand = "" + new Double((d%100000000)/10000).intValue();

        //type:1>保留两位小数  2>double->int 取整
        String yuan = null;
        if(type == 1) {
            DecimalFormat df   = new DecimalFormat("######0.00");
            yuan = "" + df.format((d%100000000)%10000);
        }
        if(type ==2) {
            yuan = "" + new Double((d%100000000)%10000).intValue();
        }
        list.add(billion);
        list.add("亿");
        list.add(tenThousand);
        list.add("万");
        list.add(yuan);
        list.add("元");
        return list;

    }
}
